<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"kriko.gitee.io","root":"/blog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="简单介绍​    GameFramework 是一个优秀的开源游戏框架，他独特的资源异步加载模式和优秀的代码结构十分值得我去学习。因此本系列文章主要记录了在学习这个框架时，我遇到的一些问题，解决思路以及一些心得体会。通过学习该框架，我希望可以提炼自己的代码整洁性，了解如何独自设计中大型项目的整理逻辑。 一、流程 有限状态机 在拿到实例程序StarForce之后，我首先做的事情就是想要找到整个游戏的">
<meta property="og:type" content="article">
<meta property="og:title" content="LearnUnityGameFrameWork">
<meta property="og:url" content="https://kriko.gitee.io/blog/2020/11/05/LearnUnityGameFrameWork/index.html">
<meta property="og:site_name" content="GameDev">
<meta property="og:description" content="简单介绍​    GameFramework 是一个优秀的开源游戏框架，他独特的资源异步加载模式和优秀的代码结构十分值得我去学习。因此本系列文章主要记录了在学习这个框架时，我遇到的一些问题，解决思路以及一些心得体会。通过学习该框架，我希望可以提炼自己的代码整洁性，了解如何独自设计中大型项目的整理逻辑。 一、流程 有限状态机 在拿到实例程序StarForce之后，我首先做的事情就是想要找到整个游戏的">
<meta property="og:image" content="https://kriko.gitee.io/blog/2020/11/05/images/procedure_01.png">
<meta property="og:image" content="https://kriko.gitee.io/blog/2020/11/05/images/procedure_02.png">
<meta property="article:published_time" content="2020-11-05T02:10:43.000Z">
<meta property="article:modified_time" content="2020-11-05T03:34:13.981Z">
<meta property="article:author" content="kriko">
<meta property="article:tag" content="Unity">
<meta property="article:tag" content="GameFrameWork">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://kriko.gitee.io/blog/2020/11/05/images/procedure_01.png">

<link rel="canonical" href="https://kriko.gitee.io/blog/2020/11/05/LearnUnityGameFrameWork/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-Hans'
  };
</script>

  <title>LearnUnityGameFrameWork | GameDev</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">GameDev</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/blog/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://kriko.gitee.io/blog/2020/11/05/LearnUnityGameFrameWork/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="kriko">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="GameDev">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LearnUnityGameFrameWork
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-11-05 10:10:43 / 修改时间：11:34:13" itemprop="dateCreated datePublished" datetime="2020-11-05T10:10:43+08:00">2020-11-05</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/Unity%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/" itemprop="url" rel="index"><span itemprop="name">Unity学习记录</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="简单介绍"><a href="#简单介绍" class="headerlink" title="简单介绍"></a>简单介绍</h1><p>​    GameFramework 是一个优秀的开源游戏框架，他独特的资源异步加载模式和优秀的代码结构十分值得我去学习。因此本系列文章主要记录了在学习这个框架时，我遇到的一些问题，解决思路以及一些心得体会。通过学习该框架，我希望可以提炼自己的代码整洁性，了解如何独自设计中大型项目的整理逻辑。</p>
<h2 id="一、流程"><a href="#一、流程" class="headerlink" title="一、流程"></a>一、流程</h2><ul>
<li><p>有限状态机</p>
<p>在拿到实例程序<code>StarForce</code>之后，我首先做的事情就是想要找到整个游戏的入口函数。整个GameFramework 设计似乎是打算通过一个有限状态机来管理各个部分的逻辑，游戏每执行到一个状态，首先卸载之前状态，然后执行该状态的初始化函数，再执行每帧更新函数。类似于状态模式的设计，是一个很不错的解耦方式。</p>
<ol>
<li>在GameFramework游戏物体下，存在一个 Procedure，可以通过这个组件来激活可用的流程。每个流程类都继承自 <code>ProcedureBase</code> ,并且可以分别实现<code>OnEnter OnLeave OnUpdate</code> 三个主要的函数，这三个函数都可以获取到<code>procedureOwner</code> 对象。</li>
<li><code>ProcedureComponent</code> 在编写完成流程脚本之后，可以看到该组件上自动将写好的流程显示出来，由于暂时没有去详细研究框架底层代码，我猜测可能是运用了类似反射的机制。不管怎样，通过<code>ProcedureComponent</code> 可以指定需要用到的流程，并将其激活。<code>Entrance Procedure</code> 表示入口流程，需要首先指定一个初试流程。</li>
</ol>
<p>为了测试该模块，我写了一个简单的<code>ProcedureLaunch</code> 类作为整个流程的起始</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">namespace Client &#123;</span><br><span class="line">    public class ProcedureLaunch : ProcedureBase</span><br><span class="line">    &#123;</span><br><span class="line">        protected override void OnInit(IFsm&lt;IProcedureManager&gt; procedureOwner) &#123;</span><br><span class="line">            base.OnInit(procedureOwner);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        protected override void OnEnter(IFsm&lt;IProcedureManager&gt; procedureOwner) &#123;</span><br><span class="line">            base.OnEnter(procedureOwner);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        protected override void OnUpdate(IFsm&lt;IProcedureManager&gt; procedureOwner, float elapseSeconds, float realElapseSeconds) &#123;</span><br><span class="line">            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);</span><br><span class="line">            ChangeState&lt;ProcedurePreload&gt;(procedureOwner);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        protected override void OnLeave(IFsm&lt;IProcedureManager&gt; procedureOwner, bool isShutdown) &#123;</span><br><span class="line">            base.OnLeave(procedureOwner, isShutdown);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>该类很简单，就是在执行一帧之后进入到下一个流程，预加载流程。在<code>ProcedureComponent</code> 激活该流程，同时在<code>Entrance Procedure</code> 指定该流程为入口。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">namespace Client &#123;</span><br><span class="line">    public class ProcedurePreload : ProcedureBase</span><br><span class="line">    &#123;</span><br><span class="line">        protected override void OnInit(IFsm&lt;IProcedureManager&gt; procedureOwner) &#123;</span><br><span class="line">            base.OnInit(procedureOwner);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        protected override void OnEnter(IFsm&lt;IProcedureManager&gt; procedureOwner) &#123;</span><br><span class="line">            base.OnEnter(procedureOwner);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        protected override void OnUpdate(IFsm&lt;IProcedureManager&gt; procedureOwner, float elapseSeconds, float realElapseSeconds) &#123;</span><br><span class="line">            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);</span><br><span class="line">            Log.Info(&quot;Ok!We are in preload procedure!&quot;);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        protected override void OnLeave(IFsm&lt;IProcedureManager&gt; procedureOwner, bool isShutdown) &#123;</span><br><span class="line">            base.OnLeave(procedureOwner, isShutdown);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img src="../images/procedure_01.png"  />

<ul>
<li>图1.1 <code>ProcedurePreload</code> 打印结果</li>
</ul>
<p>图1.1 展示了上述流程打印的结果，从Info来看，正确的执行了代码逻辑，同时我们也可以去观察一下之前提到的<code>ProcedureComponent</code> 组件。</p>
<p><img src="../images/procedure_02.png" alt=""></p>
<ul>
<li>图1.2 <code>ProcedureComponent</code> 组件显示</li>
</ul>
<p>注意到红色箭头指向，表示目前已经运行到了<code>ProcedurPreload</code>  至此一个简单的流程已经完成。</p>
<p>使用流程，可以很自然的形成一种对游戏逻辑分块的思想，比如分离登录状态，资源加载状态，游戏开始状态，主游戏状态，游戏结束状态等等。</p>
</li>
</ul>
<h2 id="DataTable"><a href="#DataTable" class="headerlink" title="DataTable"></a>DataTable</h2><ul>
<li><p>简介</p>
<p>简单的研究完流程之后，就可以更进一步学习这个框架了，毕竟是找到了源头。由于<code>StarForce</code>封装的过于复杂，我们可以先选择一个感兴趣的方向研究，这里我选择了和数据有关的DataTable。</p>
</li>
<li><p>理解</p>
</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/Unity/" rel="tag"># Unity</a>
              <a href="/blog/tags/GameFrameWork/" rel="tag"># GameFrameWork</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2020/07/14/C-Multithreading/" rel="prev" title="C++多线程编程（基于WindowsAPI）">
      <i class="fa fa-chevron-left"></i> C++多线程编程（基于WindowsAPI）
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#简单介绍"><span class="nav-number">1.</span> <span class="nav-text">简单介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#一、流程"><span class="nav-number">1.1.</span> <span class="nav-text">一、流程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DataTable"><span class="nav-number">1.2.</span> <span class="nav-text">DataTable</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">kriko</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">3</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kriko</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  















  

  

</body>
</html>
